var express = require('express');
var router = express.Router();

const alipaySdk = require('../db/alipayUtil')
const AlipayFormData = require('alipay-sdk/lib/form').default;
const cors = require('cors')
router.use(cors())
router.use(express.urlencoded({extended:true}))

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.post('/api/payment',function(req,res,next){

  //前端给后端的数据
  let orderId = req.body.orderId;
  console.log(orderId);
  //对接支付宝
  const formData = new AlipayFormData();
  formData.setMethod('get');

  formData.addField('returnUrl','http://localhost:5173/');
  formData.addField('bizContent',{
    outTradeNo:new Date(),
    productCode:'FAST_INSTANT_TRADE_PAY',
    totalAmount:'0.01',
    subject:'666',
    // body:'777'
  });

  const result = alipaySdk.exec(
    'alipay.trade.page.pay',
    {},
    {formData:formData}
  );

  result.then((resp)=>{
    res.send({
      success:'true',
      code:200,
      'result':resp
    })
  })


})
module.exports = router;
